package com.qianchang.microfinance.view.credit.data;


import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dm.library.adapter.CommonAdapter;
import com.dm.library.adapter.CommonViewHolder;
import com.lzy.imagepicker.bean.ImageItem;
import com.qianchang.microfinance.R;

import java.util.List;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: zhoulai
 * 修改时间: 2017/7/21 14:19
 */


public class CompleteMaterialDetailAdapter extends CommonAdapter<ImageItem> implements CommonAdapter.MultiItemTypeSupport {
    private static final int ADD = 0;
    private static final int IMAGE = 1;
    private boolean canAdd = true;
    private OnDeleteClick onDeleteClick;

    public CompleteMaterialDetailAdapter(Context mContext, List<ImageItem> beanList) {
        super(mContext, beanList, R.layout.adapter_complete_material_photo);
        setTypeSupport(this);
    }

    @Override
    protected void convert(CommonViewHolder holder, final ImageItem imageItem, final int position) {
        if (position == getItemCount() - 1) {
            if (canAdd) {
                holder.itemView.setVisibility(View.VISIBLE);
            } else {
                holder.itemView.setVisibility(View.GONE);
            }
        } else {
            ImageView ivImage = holder.getView(R.id.iv_image);
            ivImage.setTag(null);
            String path = imageItem.getPath();
//            if (null != path && path.startsWith(DMConstant.Config.BASE_URL)) {
//                holder.setVisible(R.id.iv_delete, false);
//            } else {
//                holder.setVisible(R.id.iv_delete, true);
//            }
            Glide.with(mContext).load(path).centerCrop().error(R.drawable.img_jzsb).into(ivImage);
            holder.setOnClickListener(R.id.iv_delete, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getItemList().remove(getItemList().get(position));
                    notifyDataSetChanged();
                    canAdd = true;
                    notifyDelete(imageItem);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return super.getItemCount() + 1;
    }


    public void setShowAdd(boolean canAdd) {
        this.canAdd = canAdd;
        notifyDataSetChanged();
    }

    @Override
    public int getLayoutId(int itemType) {
        if (itemType == ADD) {
            return R.layout.adapter_complete_material_add;
        } else {
            return R.layout.adapter_complete_material_photo;
        }
    }

    @Override
    public int getItemType(int position, Object o) {
        if (position == getItemCount() - 1) {
            return ADD;
        } else {
            return IMAGE;
        }
    }

    public interface OnDeleteClick {
        void onDelete(ImageItem imageItem);
    }

    public void setOnDeleteListener(OnDeleteClick onDeleteClick) {
        this.onDeleteClick = onDeleteClick;
    }

    public void notifyDelete(ImageItem imageItem) {
        if (null != onDeleteClick) {
            onDeleteClick.onDelete(imageItem);
        }
    }
}
